const db = require("../db/index");
// 获取
exports.getMerchants = (req, res) => {
  const sql = `select * from ev_merchant where is_delete=0 order by id asc `;
  db.query(sql, (err, results) => {
    // 1. 执行 SQL 语句失败
    if (err) return res.cc(err);

    // 2. 执行 SQL 语句成功
    res.send({
      status: 0,
      message: "获取商家列表成功！",
      data: results,
      total: results.length,
    });
  });
};
// 添加
exports.addMerchant = (req, res) => {
  const stuInfo = {
    ...req.body,
    date: new Date(),
  };
  const sql = `insert into ev_merchant set ?`;
  db.query(sql, stuInfo, (err, results) => {
    if (err) return res.cc(err);
    res.send({ status: 0, message: "新增商家数据成功!" });
  });
};
// 删除
exports.delMerchant = (req, res) => {
  const sql = `update ev_merchant set is_delete = 1 where id=?`;
  db.query(sql, req.params.id, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("删除商家数据失败!");
    res.send({ status: 0, message: "删除商家数据成功!" });
  });
};
// 更新
exports.updateMerchant = (req, res) => {
  const sql = `update ev_merchant set ? where id=?`;
  db.query(sql, [req.body, req.body.id], (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("更新商家数据失败!");

    res.send({ status: 0, message: "更新商家数据成功!" });
  });
};
